package com.elisoft.emlap.common;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wuyanshen
 * @Date 2018-12-12 2:05 PM
 * @Discription 统一接口返回对象
 */
@Data
@NoArgsConstructor
@Slf4j
public class Result implements Serializable {
    private String code;
    private String message;
    private Object data;

    public Result(String code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    //封装成功返回消息，不带data
    public static Result success(){
        return new Result("200", "请求成功", null);
    }

    //封装成功返回消息，只有message
    public static Result success(String message){
        return new Result("200", message, null);
    }

    //封装成功返回消息，只有data
    public static Result success(Object data){
        return new Result("200", "请求成功", data);
    }

    //封装成功返回消息，带message，带data
    public static Result success(String message,Object data){
        return new Result("200", message, data);
    }

    //封装失败返回消息，不带message
    public static Result fail(){
        return new Result("500", "请求失败",null);
    }

    //封装失败返回消息，带message
    public static Result fail(String message){
        return new Result("500", message,null);
    }

    public static void main(String[] args) {
        Map map = new HashMap<>();
        map.put("id", "1");
        map.put("name", "小米");
        map.put("age", "22");
        String str = JSON.toJSONString(Result.success(map));
        log.info(str);
    }
}
